package com.hainatong.jcmvvmbase.mainhome.fragment.holiday;

import android.os.Bundle;
import android.view.View;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.jcmvvmbase.MvvmLazyFragment;
import com.hainatong.jcmvvmbase.R;
import com.hainatong.jcmvvmbase.databinding.FragmentNormalHolidayBinding;
import com.hainatong.jcmvvmbase.utils.TimeUtil;
import com.hainatong.jcmvvmbase.widget.PickerDialog;
import com.hainatong.jcmvvmbase.widget.datapicker.CustomDatePicker;
import com.hainatong.mvvmbase.base.NoViewModel;

public class NormalHolidayFragment extends MvvmLazyFragment<FragmentNormalHolidayBinding, NoViewModel>
{
    private static String HOLIDAY_TAG = "NormalHolidayFragment";

    public static int  normalHolidayType = 1;
    public static int  tempHolidayType = 2;
    public static int  delayHolidayType = 3;
    public static int  changeHolidayType = 4;

    private int holidayType;
    private int timeTag = 0;
    private CustomDatePicker datePicker;

    private PickerDialog pickerDialog;

    public static NormalHolidayFragment newInstance(String typeCode)
    {
        Bundle bundle = new Bundle();
        bundle.putString( HOLIDAY_TAG , typeCode );
        NormalHolidayFragment holidayFragment = new NormalHolidayFragment();
        holidayFragment.setArguments( bundle );
        return holidayFragment;
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_normal_holiday;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        String type = "";
        if(  args != null )
        {
            type = args.getString(HOLIDAY_TAG , "");
        }

        if( !StringUtils.isEmpty( type ))
        {
            holidayType = Integer.parseInt( type );
        }
    }

    @Override
    protected void onRetryBtnClick() {

    }

    @Override
    protected void initView(View view)
    {
        pickerDialog = new PickerDialog(getActivity(), (selectStr, position) -> {

            viewDataBinding.tvArea.setText( selectStr );
            ToastUtils.showShort( selectStr );

        });

        initDataPicker();

        viewDataBinding.btnSelect1.setOnClickListener( v -> {
            timeTag = 1;
            datePicker.show( TimeUtil.getTimeStandard( System.currentTimeMillis() ) );
        } );
        viewDataBinding.btnSelect2.setOnClickListener( v -> {
            timeTag = 2;
            datePicker.show( TimeUtil.getTimeStandard( System.currentTimeMillis() ) );
        } );

        viewDataBinding.btnSelect3.setOnClickListener( v -> {

            pickerDialog.showPickContent(new String[]{"泉州港","海口港","上海港","天津港"});

        } );

        initViewByType(holidayType);
    }

    private void initViewByType(int type)
    {
        switch (type)
        {
            case 1:
                viewDataBinding.rlTempHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlZhengshuHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayStartTimeLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayEndTimeLayout.setVisibility(View.GONE);
                break;
            case 2:
                viewDataBinding.rlTempHolidayLayout.setVisibility(View.VISIBLE);
                viewDataBinding.rlZhengshuHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayStartTimeLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayEndTimeLayout.setVisibility(View.GONE);
                break;
            case 3:
                viewDataBinding.rlTempHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlZhengshuHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayStartTimeLayout.setVisibility(View.VISIBLE);
                viewDataBinding.rlDelayEndTimeLayout.setVisibility(View.VISIBLE);
                break;
            case 4:
                viewDataBinding.rlTempHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlZhengshuHolidayLayout.setVisibility(View.VISIBLE);
                viewDataBinding.rlDelayStartTimeLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayEndTimeLayout.setVisibility(View.GONE);
                break;
            default:
                viewDataBinding.rlTempHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlZhengshuHolidayLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayStartTimeLayout.setVisibility(View.GONE);
                viewDataBinding.rlDelayEndTimeLayout.setVisibility(View.GONE);
                break;
        }
    }

    private void initDataPicker()
    {
        datePicker = new CustomDatePicker(getActivity(), new CustomDatePicker.ResultHandler() {
            @Override
            public void handle(String time) {
                if( timeTag == 1 )
                {
                    // 开始时间
                    viewDataBinding.tvStartTime.setText(  TimeUtil.getTimeYMDHS2YMD( time )  );
                }
                else
                {
                    viewDataBinding.tvEndTime.setText( TimeUtil.getTimeYMDHS2YMD( time ) );
                }

                // 获取时间 一个long值
                //long resultTime = TimeUtil.parseDate(time).getTime();
                LogUtils.e("CustomDatePicker", time + "  时间= " + TimeUtil.getTimeYMDHS2YMD( time ));
            }
        } , "2020-01-01 00:00" ,  TimeUtil.getTimeStandard((System.currentTimeMillis() + 1000*60*60*24*365)));

        // 初始化日期格式请用：yyyy-MM-dd HH:mm
        // HH:mm，否则不能正常运行 //false 不显示时和分
        datePicker.showSpecificTime(true);
        datePicker.setIsLoop(true); // false 不允许循环滚动
        /*
         * //currentTime.setText(now); datePicker = new CustomDatePicker(this, new
         * CustomDatePicker.ResultHandler() {
         * @Override public void handle(String time) { // 回调接口，获得选中的时间 //currentTime.setText(time);
         * } }, "2010-01-01 00:00", now); // 初始化日期格式请用：yyyy-MM-dd HH:mm，否则不能正常运行
         * datePicker.showSpecificTime(true); // 显示时和分 datePicker.setIsLoop(true); // 允许循环滚动
         */
    }

    @Override
    protected void lazyLoad()
    {


    }

}
